package com.payment.utils;

import com.alibaba.fastjson.JSON;
import com.payment.entity.pay.paypalpay.response.ResponsePaypalPay;
import com.payment.entity.pay.paypalpay.response.ResponsePaypalPayData;
import lombok.extern.slf4j.Slf4j;

/**
 * 公共类
 *
 * @author lm
 * @date 2021/7/21 16:38
 */
@Slf4j
public class UtilsInterfacesPackage {

    /**
     * 计算签名
     *
     * @param str
     * @return
     */
    public String calculateSignature(String str) {
        String sign = MD5.calcMD5(str);
        return sign;
    }
    //签名不通过回复公共报文

    public String publicMessage(String connSeqNo, String tradeTime, String partnerCode, String token, String tradeType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1111);
        data.setTradeRstMessage("The signature is incorrect, please confirm the signature;签名有误,请确认签名!");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);

        return jsonString;
    }

    public String publicIsopen(String connSeqNo, String tradeTime, String partnerCode, String token, String tradeType) {
        ResponsePaypalPayData opendata = new ResponsePaypalPayData();
        opendata.setConnSeqNo(connSeqNo);
        opendata.setPartnerCode(partnerCode);
        opendata.setToken(token);
        opendata.setTradeType(tradeType);
        opendata.setTradeTime(tradeTime);
        opendata.setTradeRstCode(1112);
        opendata.setTradeRstMessage("The current order is in the open state;当前订单已经是开通状态");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay responseOpen = new ResponsePaypalPay();
        responseOpen.setData(opendata);
        responseOpen.setSign(MD5.calcMD5(JSON.toJSONString(opendata)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(responseOpen);

        return jsonString;
    }

    /**
     * 专门用于合作伙伴签名不通过的报文回复
     *
     * @param connSeqNo
     * @param tradeTime
     * @param partnerCode
     * @param token
     * @return
     */

    public String signatureFailMessage(String connSeqNo, String tradeTime, String partnerCode, String token) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1111);
        data.setTradeRstMessage("Signature verification failed;请求签名验证失败");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);

        return jsonString;
    }

    /**
     * 微信用户用openid查询设备号和设备类型时，openid为null或者为null
     *
     * @param connSeqNo
     * @param tradeTime
     * @param partnerCode
     * @param token
     * @param tradeType
     * @return
     */
    public String openIdIsNull(String connSeqNo, String tradeTime, String partnerCode, String token, String tradeType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeRstCode(1113);
        data.setTradeRstMessage("Only WeChat users can query;仅微信用户可查询");
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);

        ResponsePaypalPay responseiccId = new ResponsePaypalPay();
        responseiccId.setData(data);
        responseiccId.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        /*返回给调用者的报文*/
        String jsonString = JSON.toJSONString(responseiccId);
        log.info("回复的报文是：" + jsonString);
        return jsonString;
    }

    public String FailMessageTokenOrPartnerCode(String connSeqNo, String tradeTime, String partnerCode, String token, String deviceId, String deviceType) {

        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1114);
        data.setTradeRstMessage("Token/partnerCode deviceType is wrong;token/partnerCode/deviceType有误");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;

    }

    public String FailMessageDeviceIdOrDeviceType(String connSeqNo, String tradeTime, String partnerCode, String token, String deviceId, String deviceType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1115);
        data.setTradeRstMessage("DeviceId or deviceType is illegal;deviceId或者deviceType不合法");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }

    public String dataWeiKong(String ConnSeqNo, String createTime) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(ConnSeqNo);
        data.setTradeTime(createTime);
        data.setTradeRstCode(1116);
        data.setTradeRstMessage("Illegal request;请求报文不合法");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }

    public String requestCannotRespondMessage(String connSeqNo, String partnerCode, String tradeTime, String token, String tradeType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeTime(tradeTime);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeRstCode(1117);
        data.setTradeRstMessage("No response data;请求无响应数据！");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }

    /**
     * 验证当前token是不是当前渠道的
     *
     * @param connSeqNo
     * @param partnerCode
     * @param tradeTime
     * @param token
     * @param tradeType
     * @return
     */
    public String NoOrderListMessage(String connSeqNo, String partnerCode, String tradeTime, String token, String tradeType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1003);
        data.setTradeRstMessage("The order was not queried;未查询到订单");

        ResponsePaypalPay responseQueryorder = new ResponsePaypalPay();
        responseQueryorder.setData(data);
        responseQueryorder.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(responseQueryorder);
        log.info("响应报文：" + jsonString);
        return jsonString;
    }

    public String tokenMessage(String connSeqNo, String tradeTime, String partnerCode, String token, String tradeType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1118);
        data.setTradeRstMessage("Token Validation is wrong;Token验证有误");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);

        return jsonString;
    }

    public String duplicateRequestMessage(String connSeqNo, String partnerCode, String tradeTime, String token, String tradeType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setToken(token);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode(1119);
        data.setTradeRstMessage("Duplicate request；重复的请求");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();

        resPackages.setData(data);

        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);

        return jsonString;
    }

    public String unknownError(String connSeqNo, String curtime, String tradeType) {
        System.out.println("接口请求未获得数据返回");
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode(9999);
        data.setTradeRstMessage("接口出现未知错误，未成功返回！");

        ResponsePaypalPay userLogin = new ResponsePaypalPay();
        userLogin.setData(data);
        userLogin.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(userLogin);
        log.info("响应报文：" + jsonString);
        return jsonString;
    }
}
